package com.ruoyi;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import org.junit.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

@SpringBootTest
public class Main {

    @Test
    public void getip() throws UnknownHostException {
        String ip = InetAddress.getLocalHost().getHostAddress();
        System.out.println(ip);
    }

    @Test
    public void test() {
        for (int i = 1; i <= 7; i++) {
            int tomorrow = getTomorrow(i);
            tomorrow = getTomorrow(tomorrow);
            if (tomorrow == 5) {
                System.out.println(i);
            }
        }
    }

    int getTomorrow(int i) {
        int i1 = i + 1;
        if (i1 == 8) {
            return 1;
        }
        return i1;
    }

    @Test
    public void test1() throws Exception {


        long l1 = getTimeDifference();
        System.out.println(l1);
        long time = DateUtil.parseDateTime("2020-12-21 14:59:00").getTime();
        for (int j = 0; j < 5; j++) {

            for (int i = -5; i < 5; i++) {

                int finalI = i;
                ThreadUtil.execAsync(new Runnable() {
                    @SneakyThrows
                    @Override
                    public void run() {
                        send(time, l1 + finalI);
                    }
                }, true);
            }
        }

        ThreadUtil.sleep(1000 * 60);
    }

    @Test
    public void toTime() {
        Date dateTime = new Date(1608522720198L);
        String format = DateUtil.format(dateTime, "yyyy-HH-mm HH:mm:ss.SSS");
        System.out.println(format);
        final Long[] l = {0L};
        for (int i = 0; i < 1000; i++) {
            ThreadUtil.execute(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        Date dateTime = new Date();
                        String format = DateUtil.format(dateTime, "yyyy-HH-mm HH:mm:ss.SSS");
                        HttpRequest get = HttpUtil.createGet("https://39.96.250.171/assets/css/main.css");
                        get.header("Host","itnanls.cn");
                        get.setReadTimeout(1000*60*60);
                        int length = get.execute().body().getBytes(StandardCharsets.UTF_8).length;
                        l[0] +=length;
                    }
                }
            });
        }

        while (true){

            System.out.println(  l[0]/1024);
            ThreadUtil.sleep(5000);
        }

    }

    Long getTimeDifference() throws IOException {
        Long l1 = 0L;
        Socket socket = new Socket("pro.jd.com", 80);
        socket.setSoTimeout(6000000);

        // 要发送给服务器的信息
        OutputStream os = socket.getOutputStream();
        PrintWriter pw = new PrintWriter(os);
        pw.write("GET https://pro.jd.com/mall/active/3ZYfZKGRAhbHzJySpRriJoGWo8v6/index.html?innerAnchor=58947736707&focus=3 HTTP/1.1\n" +
                "User-Agent: PostmanRuntime/7.26.8\n" +
                "Accept: */*\n" +
                "Postman-Token: 3fda5d62-c481-4765-ac30-ef3dda5e2fec\n" +
                "Accept-Encoding: gzip, deflate, br\n" +
                "Connection: keep-alive\n" +
                "Referer: http://pro.jd.com/mall/active/3ZYfZKGRAhbHzJySpRriJoGWo8v6/index.html?innerAnchor=58947736707&focus=3\n" +
                "Host: pro.jd.com\n" +
                "\n");

        pw.flush();
        socket.shutdownOutput();

        long l = System.currentTimeMillis();
        // 从服务器接收的信息
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String info = null;
        while ((info = br.readLine()) != null) {

            boolean contains = info.contains("{\"code\"");
            if (contains) {
                Long aLong = JSONUtil.parseObj(info).getLong("data");
                l1 = l - aLong;
            }
                System.out.println("我是客户端，服务器返回信息：" + info);

        }
        br.close();
        is.close();
        os.close();
        pw.close();
        socket.close();

        return l1;
    }

    void send(Long time, Long l) throws IOException {
        Long l1 = 0L;
        Socket socket = new Socket("172.20.20.18", 8081);
        socket.setSoTimeout(6000000);

        // 要发送给服务器的信息
        OutputStream os = socket.getOutputStream();
        PrintWriter pw = new PrintWriter(os);
        pw.write("GET http://172.20.20.18:8081/spikeTest?name=111 HTTP/1.1\n" +
                "User-Agent: PostmanRuntime/7.26.8\n" +
                "Accept: */*\n" +
                "Postman-Token: 00242693-a4e4-49a4-a19a-d89c30b2f873\n" +
                "Host: 172.20.20.18:8081\n" +
                "Accept-Encoding: gzip, deflate, br\n" +
                "Connection: keep-alive\n" +
                "\n");
        while (true)
            if (System.currentTimeMillis() - l > time) {
                break;
            }
        pw.flush();
        socket.shutdownOutput();

        // 从服务器接收的信息
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String info = null;
        while ((info = br.readLine()) != null) {
            //System.out.println("我是客户端，服务器返回信息：" + info);
            boolean contains = info.contains("{\"code\"");
            if (contains) {
                System.out.println("我是客户端，服务器返回信息：" + info);
            }
        }
        br.close();
        is.close();
        os.close();
        pw.close();
        socket.close();


    }


    @Test
    public void test2() throws Exception {

        String s = "7b2254797065223a22736563703235366b31222c22507269766174654b6579223a2249304a37674a7673746c475665785a69794a714b504b44384c714f39444d5434547069782b36376974356f3d227d";
         byte[] byte2 = getByte2(s);
        System.out.println(Base64.encode(byte2));
    }

    static  byte[] getByte2(String s) {
        byte[] bytes = new byte[s.length()/2];
        int j=0;
        for (int i = 0; i < s.length(); i=i+2) {
            bytes[j++] = (byte) Integer.parseInt(s.substring(i,i+2), 16);
        }
        return bytes;
    }
}
